package com.lry.basic.algorithm.list;

/**
 * @author:刘仁有
 * @desc:
 * @email:953506233@qq.com
 * @data:2020/1/6
 */
public class LinkedStack {

    private Node top = null;
    private int size;
    public LinkedStack() {}

    public void push(int x) {
        Node node = null;
        if(null==top){
             node = new Node(x,null);
        }else{
             node = new Node(x,top);
        }
        top = node;
        size++;
    }

    public void pop() {
        if(top!=null){
            top = top.next;
            size--;
        }
    }

    public int size(){
        return size;
    }

    public boolean isEmpty(){
        return size>0?false:true;
    }


    public int peek() {
        if(top!=null){
            return top.data;
        }
        throw new RuntimeException("stack is empty");
    }



    class Node{
        public int data;
        public Node next;
        public Node(int data,Node next){
            this.data = data;
            this.next = next;
        }
    }
}
